package com.bjdd.jdy.ui.withdraw;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.BankCardBean;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;
import com.zhy.autolayout.AutoLinearLayout;

import java.math.BigDecimal;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithDrawActivity extends BaseActivity<WithDrawPresenter, WithDrawModel> implements WithDrawContract.View {

    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.tv_bank_card)
    TextView tvBankCard;
    @Bind(R.id.tv_card_name)
    TextView tvCardName;
    @Bind(R.id.tv_can_withdraw)
    TextView tvCanWithdraw;
    @Bind(R.id.tv_withdraw_all)
    TextView tvWithdrawAll;
    @Bind(R.id.btn_withdraw)
    Button btnWithdraw;
    @Bind(R.id.all_content)
    AutoLinearLayout allContent;
    @Bind(R.id.et_money)
    EditText etMoney;
    @Bind(R.id.all_withdraw_success)
    AutoLinearLayout allWithDrawSuccess;
    @Bind(R.id.tv_card_type)
    TextView tvCardType;
    @Bind(R.id.tv_withdraw_money)
    TextView tvWithDrawMoney;
    private List<BankCardBean> banks;

    @Override
    public int getLayoutId() {
        return R.layout.activity_with_draw;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.toolbar_bg), 0);
        title.setText("提现");
        allContent.setVisibility(View.GONE);
    }


    @OnClick({R.id.toolbar_back, R.id.tv_withdraw_all, R.id.btn_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                finish();
                break;
            case R.id.tv_withdraw_all:
                etMoney.setText(SpUtil.getUser().getMoney()+"");
                break;
            case R.id.btn_withdraw:
                String money = etMoney.getText().toString();
                if (TextUtils.isEmpty(money) || money.equals("")){
                    T.show("请先输入提现金额");
                    return;
                }
                BigDecimal decimal = new BigDecimal(money);
                if (decimal.compareTo(SpUtil.getUser().getMoney()) == 1){
                    T.show("超出提现金额");
                    return;
                }
                presenter.withDraw(banks.get(0).getCarrierCardId()+"",money);
                break;
        }
    }

    @Override
    public void cardBack(List<BankCardBean> data) {
        banks = data;
        if (data == null || data.size() <= 0){
            JumpUtil.toBankCard(context);
            finish();
        }else {
            String c = banks.get(0).getBankcard().substring(banks.get(0).getBankcard().length() - 4);
            tvCardName.setText(banks.get(0).getBank().getBankName()+"储蓄卡("+c+")");
            tvCanWithdraw.setText("可提金额"+ SpUtil.getUser().getMoney()+"元,");
            allContent.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void withDrawSuccess(String money) {
        String c = banks.get(0).getBankcard().substring(banks.get(0).getBankcard().length() - 4);
        tvCardType.setText(banks.get(0).getBank().getBankName()+" 尾号"+c);
        tvWithDrawMoney.setText("￥"+money);
        allContent.setVisibility(View.GONE);
        allWithDrawSuccess.setVisibility(View.VISIBLE);
    }
}
